#pragma once

#include<iostream>
#include<functional>
#include<vector>

using task_t=std::function<void()>;//#include<functioanl>

class TaskManger{
public:
	TaskManger(){
		srand(time(nullptr));
		tasks.push_back([](){
			std::cout<<"sub process["<<"]执行访问数据库的任务\n"<<std::endl;
		});
		tasks.push_back([](){
			std::cout<<"sub process["<<"]执行url解析\n"<<std::endl;
		});
		tasks.push_back([](){
			std::cout<<"sub process["<<"]执行加密任务\n"<<std::endl;
		});
		tasks.push_back([](){
			std::cout<<"sub process["<<"]执行数据持久化任务\n"<<std::endl;
		});
	}

	int Select(){
		return rand()%tasks.size();
	}

	void Excute(unsigned long num){
		if(num<0||num>=tasks.size())
			return;

		tasks[num]();
	}

	~TaskManger(){}
private:
	std::vector<task_t> tasks;
};

TaskManger tm;

void Worker(){
	while(true){
		int cmd=0;
		int n=::read(0,&cmd,sizeof(cmd));
		if(n==sizeof(cmd)){
			tm.Excute(n);
		}
		else if(n==0){
			std::cout<<"pid:"<<getpid()<<" quit..."<<std::endl;
			break;
		}
		else{}
	}
}